<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        "use strict"

        var dog={name:"狗狗",_age:0};  //数据属性

        Object.defineProperty(dog,"age",{
            configurable:false,  //不允许删除
            enumerable:false,  //不能枚举
            get:function(){  //读
                return this._age;
            },
            set:function(value){  //写
                if(!isNaN(value)&&value>=0&&value<=100){
                    this._age=value;
                }
                else{
                    throw "年龄必须是0-100的数字";
                }
            }
        });

        console.log(dog.age);
        dog.age=5;
        console.log(dog.age);
        dog.age="five";

        dog._age=9;
    </script>
</body>
</html>